package com.poetry.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poetry.constant.RedisConstant;
import com.poetry.domain.po.Author;
import com.poetry.exception.BasePage;
import com.poetry.exception.R;
import com.poetry.mapper.AuthorMapper;
import com.poetry.service.IAuthorService;
import com.poetry.util.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author>
        implements IAuthorService {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public R<Author> queryById(Integer id) {
        Author author = redisUtil.queryWithPassThrough(RedisConstant.CACHE_AUTHOR_KEY, id, Author.class,
                this::getById, RedisConstant.CACHE_AUTHOR_TTL, TimeUnit.MINUTES);
        if (author == null) {
            return R.error("诗词不存在");
        }
        return R.success("查询诗词信息成功", author);
    }

    @Override
    public R<List<Author>> queryAuthorByPage(BasePage basePage) {
        // 页码
        int pageNum = basePage.getPageNum();
        // 页数
        int pageSize = basePage.getPageSize();
        // 内容
        String query = basePage.getQuery();
        // 条件
        String condition = basePage.getCondition();
        Page<Author> pageInfo = new Page<>(pageNum, pageSize);
        if (StringUtils.isBlank(condition) || StringUtils.isBlank(query)) {
            LambdaQueryWrapper<Author> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(Author::getId);
            Page<Author> page = this.page(pageInfo, queryWrapper);
            if (page.getTotal() == 0) {
                return R.error("作者列表为空");
            }
            return R.success("获取作者列表成功", pageInfo.getRecords());

        }
        QueryWrapper<Author> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.like(condition, query);
        Page<Author> page = this.page(pageInfo, queryWrapper1);
        if (page.getTotal() == 0) {
            return R.error("诗词列表为空");
        }
        return R.success("获取作者列表成功", pageInfo.getRecords());
    }

    @Override
    @Transactional
    public R<String> addAuthor(Author author) {
        if (author.getName() == null || StringUtils.isBlank(author.getName())) {
            return R.error("作者姓名不能为空");
        }
        if (author.getDynasty() == null || StringUtils.isBlank(author.getDynasty())) {
            return R.error("作者朝代信息不能为空");
        }
        boolean isSuccess = this.save(author);
        if (!isSuccess) {
            return R.error("作者信息添加失败");
        }
        return R.success(null, "作者信息添加成功");
    }

    @Override
    @Transactional
    public R<Author> updateAuthor(Author author) {
        if (author.getName() == null || StringUtils.isBlank(author.getName())) {
            return R.error("作者姓名不能为空");
        }
        if (author.getDynasty() == null || StringUtils.isBlank(author.getDynasty())) {
            return R.error("作者朝代信息不能为空");
        }
        Author oldAuthor = this.getById(author.getId());
        if (oldAuthor == null) {
            return R.error("作者不存在");
        }
        boolean isSuccess = this.updateById(author);
        if (!isSuccess) {
            return R.error("作者信息更新失败");
        }
        stringRedisTemplate.delete(RedisConstant.CACHE_AUTHOR_KEY + author.getId());
        return R.success("作者信息更新成功", author);
    }

    @Override
    @Transactional
    public R<String> deleteBatchByIds(List<Integer> ids) {
        boolean isSuccess = this.removeByIds(ids);
        if (!isSuccess) {
            return R.error("删除失败");
        }
        stringRedisTemplate.delete(RedisConstant.CACHE_AUTHOR_KEY + ids);
        return R.success(null, "删除成功");
    }
}
